angular.module('starter.services', [])

    .factory('Chats', function () {
        // Might use a resource here that returns a JSON array

        // Some fake testing data
        var chats = [{
            id: 0,
            name: 'Ben Sparrow',
            lastText: 'You on your way?',
            face: 'https://pbs.twimg.com/profile_images/514549811765211136/9SgAuHeY.png'
        }, {
            id: 1,
            name: 'Max Lynx',
            lastText: 'Hey, it\'s me',
            face: 'https://avatars3.githubusercontent.com/u/11214?v=3&s=460'
        }, {
            id: 2,
            name: 'Adam Bradleyson',
            lastText: 'I should buy a boat',
            face: 'https://pbs.twimg.com/profile_images/479090794058379264/84TKj_qa.jpeg'
        }, {
            id: 3,
            name: 'Perry Governor',
            lastText: 'Look at my mukluks!',
            face: 'https://pbs.twimg.com/profile_images/598205061232103424/3j5HUXMY.png'
        }, {
            id: 4,
            name: 'Mike Harrington',
            lastText: 'This is wicked good ice cream.',
            face: 'https://pbs.twimg.com/profile_images/578237281384841216/R3ae1n61.png'
        }];

        return {
            all: function () {
                return chats;
            },
            remove: function (chat) {
                chats.splice(chats.indexOf(chat), 1);
            },
            get: function (chatId) {
                for (var i = 0; i < chats.length; i++) {
                    if (chats[i].id === parseInt(chatId)) {
                        return chats[i];
                    }
                }
                return null;
            }
        };
    })

    .factory('ApiService', ['ResourceService', 'MapService', function (ResourceService, MapService) {
        return {
            getChatList: function (callback) {
                var res = ResourceService.getChatList().map(MapService.mapChatList);

                if(callback){
                    callback(res);
                }
            },

            getIndexData: function (callback) {
                var res = ResourceService.getIndexData().map(MapService.mapBanner);

                 if(callback)
                    callback(res);
            },

            getSportInfo: function (callback) {
                var res = ResourceService.getSportInfo();

                if(callback)
                    callback(res)
            },

            getSettingData: function (callback) {
                var res = ResourceService.getSettingData();

                if(callback)
                    callback(res);
            },

            getProjectInfo: function (callback) {
                var res = ResourceService.getProjectInfo().map(MapService.mapProject);

                if(callback)
                    callback(res);
            }
        }

    }])

    .factory('ResourceService', ['Chats', function (Chats) {
      return {
          getChatList: function() {
              var chats = Chats.all();

              return chats;
          },

           getIndexData: function() {
             var banner = [[{
                        id: 0,
                        href: '#',
                        name:'强度',
                        face: 'http://chuantu.biz/t2/11/1439033050x-1566638214.jpg'
                    }, {
                        id: 1,
                        href: '#',
                        name: "心肺功能",
                        face: 'http://chuantu.biz/t2/11/1439033142x-954497581.jpg'
                    }], [{
                        id: 2,
                        href: '#',
                        name: "瑜伽",
                        face: 'http://chuantu.biz/t2/11/1439033171x-954497581.jpg'
                    }, {
                        id: 3,
                        href: '#',
                        name: "伸展",
                        face: 'http://chuantu.biz/t2/11/1439033190x-954497581.jpg'
                    }]];
              return banner;     
           },

           getSportInfo: function() {
            var info = {
                minuteinfo: 100,
                kaluliinfo: 600,
                sportday: 30,
                sumday: 2
            };

            return info;
           },

           getSettingData: function () {
            var data = {
                language: [
                    {id:1, name:'简体中文', status: false},
                    {id:2, name:'繁体中文', status: true},
                    {id:3, name:'English', status: false}
                ],
                languageNum: 68,
                unit: [
                    {id: 1, name: '克', status: false},
                    {id: 2, name: '千克', status: true},
                    {id: 3, name: '磅', status: false}
                ],
                autoplay: true,
                autoy: false
            };

            return data;
           },

           getProjectInfo: function () {
            var data = [
                {
                    name: '练腹肌',
                    project: [
                        {
                            id: 0,
                            name: '练腹肌1',
                            avatar: 'img/banner/1.jpg' 
                        },
                        {
                            id: 1,
                            name: '练腹肌2',
                            avatar: 'img/banner/2.jpg'
                        },
                        {
                            id: 2,
                            name: '练腹肌3',
                            avatar: 'img/banner/3.jpg'
                        },
                        {
                            id: 3,
                            name: '练腹肌4',
                            avatar: 'img/banner/4.jpg'
                        }
                    ]
                },
                {
                    name: '心肺功能',
                    project: [
                        {
                            id: 0,
                            name: '心肺功能1',
                            avatar: 'img/banner/1.jpg' 
                        },
                        {
                            id: 1,
                            name: '心肺功能',
                            avatar: 'img/banner/2.jpg'
                        },
                        {
                            id: 2,
                            name: '心肺功能3',
                            avatar: 'img/banner/3.jpg'
                        },
                        {
                            id: 3,
                            name: '心肺功能4',
                            avatar: 'img/banner/4.jpg'
                        }
                    ]
                },
                {
                    name: '瑜伽',
                    project: [
                        {
                            id: 0,
                            name: '瑜伽1',
                            avatar: 'img/banner/1.jpg' 
                        },
                        {
                            id: 1,
                            name: '瑜伽2',
                            avatar: 'img/banner/2.jpg'
                        },
                        {
                            id: 2,
                            name: '瑜伽3',
                            avatar: 'img/banner/3.jpg'
                        },
                        {
                            id: 3,
                            name: '瑜伽4',
                            avatar: 'img/banner/4.jpg'
                        }
                    ]
                },
                {
                    name: '伸展',
                    project: [
                        {
                            id: 0,
                            name: '伸展1',
                            avatar: 'img/banner/1.jpg' 
                        },
                        {
                            id: 1,
                            name: '伸展2',
                            avatar: 'img/banner/2.jpg'
                        },
                        {
                            id: 2,
                            name: '伸展3',
                            avatar: 'img/banner/3.jpg'
                        },
                        {
                            id: 3,
                            name: '伸展4',
                            avatar: 'img/banner/4.jpg'
                        }
                    ]
                }
            ];

            return data;
           }
      }
    }])

    .factory('MapService', [function () {
        var mapService = {
            mapChatList: function (c) {
                return {
                    id: c.id,
                    name: c.name,
                    lastText: c.lastText,
                    face: c.face
                };
            },

            mapBanner: function (b) {
               return b.map(function (s) {
                    return {
                        id: s.id,
                        href: s.href,
                        name: s.name,
                        atthment: s.face
                    };
                })
            },

            mapProject: function (p) {
                return {
                    name: p.name,
                    project: p.project.map(function (x) {
                        return {
                            id: x.id,
                            name: x.name,
                            avatar: x.avatar
                        }
                    })
                }
            }
        }
       return mapService;
    }])

;
